﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace orderCreater
{
    public partial class Form1 : Form
    {
        public OrderCreaterModel OCM = new OrderCreaterModel(ConfigurationManager.AppSettings["ConnStr-531"]);
        public OrderCreaterModel OCM_WDH = new OrderCreaterModel(ConfigurationManager.AppSettings["ConnStr-wdh"]);
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            this.sr_bgw.WorkerSupportsCancellation = true;
            this.sr_wdh.WorkerSupportsCancellation = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            this.sr_bgw.RunWorkerAsync();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            this.sr_wdh.RunWorkerAsync();
        }
        private void sr_bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "]  start\n");
            //string[] strs1 = File.ReadAllLines(@"d:\ac.txt");
            //List<string> strs_new = new List<string>();
            //foreach (var str in strs1) {
            //    string[] strs = str.Split(',');

            //    if (strs.Length >= 4) {
            //        strs_new.Add(strs[1] + ",\"" + strs[0] + "\",\"" + strs[2] + "\"," + strs[3]);
            //    }
            //    if (strs.Length >= 7)
            //    {
            //        strs_new.Add(strs[4] + ",\"" + strs[0] + "\",\"" + strs[5] + "\"," + strs[6]);
            //    }
            //    if (strs.Length >= 10)
            //    {
            //        strs_new.Add(strs[7] + ",\"" + strs[0] + "\",\"" + strs[8] + "\"," + strs[9]);
            //    }
            //    if (strs.Length >= 13)
            //    {
            //        strs_new.Add(strs[10] + ",\"" + strs[0] + "\",\"" + strs[11] + "\"," + strs[12]);
            //    }
            //    if (strs.Length >= 16)
            //    {
            //        strs_new.Add(strs[13] + ",\"" + strs[0] + "\",\"" + strs[14] + "\"," + strs[15]);
            //    }
            //    if (strs.Length >= 19)
            //    {
            //        strs_new.Add(strs[16] + ",\"" + strs[0] + "\",\"" + strs[17] + "\"," + strs[18]);
            //    }
            //    if (strs.Length >= 22)
            //    {
            //        strs_new.Add(strs[19] + ",\"" + strs[0] + "\",\"" + strs[20] + "\"," + strs[21]);
            //    }
            //    if (strs.Length >= 25)
            //    {
            //        strs_new.Add(strs[22] + ",\"" + strs[0] + "\",\"" + strs[23] + "\"," + strs[24]);
            //    }
            //    if (strs.Length >= 28)
            //    {
            //        strs_new.Add(strs[25] + ",\"" + strs[0] + "\",\"" + strs[26] + "\"," + strs[27]);
            //    }
            //    if (strs.Length >= 31)
            //    {
            //        strs_new.Add(strs[28] + ",\"" + strs[0] + "\",\"" + strs[29] + "\"," + strs[30]);
            //    }
            //}
            //File.WriteAllLines(@"d:\dew.txt", strs_new);
            List<Post_Msg> orders = OCM.WSYGetChangeAddrOrders();
            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] 记录数：" + orders.Count.ToString() + "\n");
            foreach (var order in orders)
            {
                WDH_AddrRange new_addr = OCM.WSYNormalAddr();
                if (new_addr.status == 200 && new_addr.data.Count > 0)
                {
                    string reciver_name = new_addr.data[0].name;
                    string addressLength = OCM.StringToByte(new_addr.data[0].address, Encoding.UTF8).Length.ToString();
                    string reciver_info = "a:6:{s:5:\"phone\";s:11:\"" + new_addr.data[0].mobile + "\";s:9:\"mob_phone\";s:11:\"" + new_addr.data[0].mobile + "\";s:9:\"tel_phone\";s:0:\"\";s:7:\"address\";s:" + addressLength + ":\"" + new_addr.data[0].address + "\";s:4:\"area\";s:0:\"\";s:6:\"street\";s:0:\"\";}";
                    string sql_order_common = string.Format("UPDATE `db_order_common` set `reciver_name`='{1}', `reciver_info`='{2}', `reciver_province_id`={3}, `reciver_city_id`={4}, `reciver_area_id`={5} where order_id={0}",
                                                            order.msg,
                                                            reciver_name,
                                                            reciver_info,
                                                            new_addr.data[0].province_id,
                                                            new_addr.data[0].city_id,
                                                            new_addr.data[0].area_id);
                    if (OCM.ExecuteNonQuery(sql_order_common))
                    {
                        Post_Msg p_msg = OCM.HandChangeAddrOrders(order.data, reciver_name, new_addr.data[0].mobile, new_addr.data[0].province_id, new_addr.data[0].city_id, new_addr.data[0].area_id, new_addr.data[0].address);
                        if (p_msg.status == 200)
                        {
                            OCM.ExecuteNonQuery("update order_change_addr set o_state=1 where ordersn=" + order.data);
                            OCM.WSYUpdateAddressCount(new_addr.data[0].id);
                            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] ordersn：" + order.data + "地址修改成功\n");
                        }
                        else
                        {
                            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] ordersn：" + order.data + "地址修改失败，" + p_msg.msg + "\n");
                        }
                    }
                }
            }
            button1.Enabled = true;

            Close();
        }
        private void sr_wdh_DoWork(object sender, DoWorkEventArgs e)
        {
            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "]  start\n");
            List<Post_Msg> orders = OCM_WDH.WDHGetChangeAddrOrders();
            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] 记录数：" + orders.Count.ToString() + "\n");
            foreach (var order in orders)
            {
                WDH_AddrNormal new_addr = OCM_WDH.WDHNormalAddr(); 
                if (new_addr.status == 200 && new_addr.data.Count > 0)
                {
                    string sql_order = string.Format("UPDATE `db_order` set `accepterName`='{1}', `accepterMobile`='{2}',`provinceId`={3},`cityId`={4},`districtId`={5},`address`='{6}' where orderId={0};",
                                                            order.msg,
                                                            new_addr.data[0].name,
                                                            new_addr.data[0].mobile,
                                                            new_addr.data[0].province_id,
                                                            new_addr.data[0].city_id,
                                                            new_addr.data[0].area_id,
                                                            OCM_WDH.FormatItemVal2(new_addr.data[0].address));
                    if (OCM_WDH.ExecuteNonQuery(sql_order))
                    {
                        Post_Msg p_msg = OCM_WDH.HandChangeAddrOrders(order.data, new_addr.data[0].name, new_addr.data[0].mobile, new_addr.data[0].province_id, new_addr.data[0].city_id, new_addr.data[0].area_id, new_addr.data[0].address);
                        if (p_msg.status == 200)
                        {
                            OCM_WDH.ExecuteNonQuery("update order_change_addr set o_state=1 where ordersn=" + order.data);
                            OCM_WDH.WDHUpdateAddressCount(new_addr.data[0].id);
                            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] WDH：" + order.data + "地址修改成功\n");
                        }
                        else
                        {
                            richTextBox1.AppendText("[" + DateTime.Now.ToLongTimeString() + "] WDH：" + order.data + "地址修改失败，" + p_msg.msg + "\n");
                        }
                    }
                }
            }
            button2.Enabled = true;
        }
        /// <summary>
        /// 判断字符串是否是数字
        /// </summary>
        public static bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^(-?[0-9]+[.]?[0-9]*)$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }
        public static bool IsNotChinese(string s)
        {
            return Regex.IsMatch(s, "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1_Click(sender,e);
        }
    }
}
